/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BusinessLayer;

import DataLayer.DataLayerInterface;
import DataLayer.dbDal;
import Objects.Game;
import com.sun.jersey.core.util.Base64;
import java.security.NoSuchAlgorithmException;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBException;

/**
 *
 * @author zorr
 */
public class BusinessLayerInterfaceHandler implements BusinessLayerInterface {

    private static BusinessLayerInterfaceHandler instance;

    private BusinessLayerInterfaceHandler() {
    }

    public static BusinessLayerInterfaceHandler getInstance() {
        if (BusinessLayerInterfaceHandler.instance == null) {
            BusinessLayerInterfaceHandler.instance = new BusinessLayerInterfaceHandler();
        }
        return BusinessLayerInterfaceHandler.instance;
    }

    @Override
    public String getGameList(String username, String pwd) {
        try {
            DataLayerInterface dli = dbDal.getInstance();
//            DataLayerInterface dil = DataLayerInterfaceHandler.getInstance();
            ArrayList<Game> result = dli.getGameList(username, pwd);
            return Transform.createDocument_getGameList(result);
        } catch (JAXBException ex) {
            Logger.getLogger(BusinessLayerInterfaceHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public String buyGame(String username, int gameId) {
        DataLayerInterface dli = dbDal.getInstance();
//            DataLayerInterface dil = DataLayerInterfaceHandler.getInstance();
        try {
            byte[] newSymKey = DRM_License_Generator.generateNewSymKey();
            Blob userCertificate = dli.getCertByUserName(username);
            if(userCertificate == null) {
                System.out.println("ich habe keinen blob ! ! ! ! ");
                System.out.println("username: "+username+" | gameid: "+gameId);
            }
            byte[] newEncSymKey = DRM_Packager.encryptSymKeywithPublicKey(newSymKey, Helper.blob2byte(userCertificate));
            if (dli.insertLicense(gameId, username, newEncSymKey, newSymKey)) {
                return Transform.createDocument_buyGame("true");
            } else {
                return Transform.createDocument_buyGame("false");
            }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(BusinessLayerInterfaceHandler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JAXBException ex) {
            Logger.getLogger(BusinessLayerInterfaceHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public String getMyGames(String username) {
        try {
            DataLayerInterface dli = dbDal.getInstance();
//            DataLayerInterface dil = DataLayerInterfaceHandler.getInstance();
            ArrayList<Game> result = dli.getMyGames(username);
            return Transform.createDocument_getMyGames(result);
        } catch (JAXBException ex) {
            Logger.getLogger(BusinessLayerInterfaceHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    @Override
    public String getLicense(String username, int gameId) {
        DataLayerInterface dli = dbDal.getInstance();
//            DataLayerInterface dil = DataLayerInterfaceHandler.getInstance();
        // hole Blob aus DB
//        Blob res = dli.getLicense(username, gameId);
            //blob to byte[]
    //        byte[] allBytesInBlob = null;
    //        try {
    //            allBytesInBlob = res.getBytes(1, (int) res.length());
    //        try {
    //            allBytesInBlob = res.getBytes(1, (int) res.length());
    //        } catch (SQLException ex) {
    //            Logger.getLogger(BusinessLayerInterfaceHandler.class.getName()).log(Level.SEVERE, null, ex);
    //        }
        try {
            // kodiere byte[] in base64
    //        String result = new String(Base64.encode(allBytesInBlob));
    //        try {
                // gib Blob als base64 zum testen auf console aus
    //            System.out.println("Blob: " + result);
                return Transform.createDocument_getLicense(Helper.blob2string64(dli.getLicense(username, gameId)));
    //        } catch (JAXBException ex) {
    //            Logger.getLogger(BusinessLayerInterfaceHandler.class.getName()).log(Level.SEVERE, null, ex);
    //        }
    //        return null;
        } catch (JAXBException ex) {
            Logger.getLogger(BusinessLayerInterfaceHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return null;
    }

    @Override
    public String getGame(String username, int gameId) {
        DataLayerInterface dli = dbDal.getInstance();
//              DataLayerInterface dil = DataLayerInterfaceHandler.getInstance();        
        //        bl bekommt von dal unverschlüsseltes spiel und symkey. 
        //        verschlüsselt spiel mit symkey und schickt das dann dem client
        
        try {
            //aus db wird das spiel geholt
            Blob game = dli.getGame(gameId);
            
            //aus db wird der symmetrische schlüssel geholt
            Blob symKey = dli.getSymKey(username, gameId);
            
            //packager verschlüsselt spiel mit symkey
            byte[] bytes = DRM_Packager.encryptGameWithSymKey(game, symKey);
            
            //base64 encoding
            String result = new String(Base64.encode(bytes));
            
            return Transform.createDocument_getGame(result);
        } catch (JAXBException ex) {
            Logger.getLogger(BusinessLayerInterfaceHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
}
